Let's do a deep dive
into container images. A container image is an archive with
files that contain your application and everything it needs to run. That might still sound a bit abstract so I want to introduce you to a very simple
example application and use that to discover the different types of files
you might find in a container image. Here's that simple example. It's a minimal Node.js
example application. There are three files here, server.js,
a package.json and an index.html. Let's explore these three files to learn
what you need to run the application. Let's take a look at what happens
in the server.js file first. This is the main configuration file,
server.js. It contains a reference to
a library dependency for express, which is a web application framework for
Node.js. It uses this to create one endpoint that
returns the contents of the index.html file and it starts to listen for
requests on port 8080. The next file is the package.json file. It's read by the tool npm, which is the Node.js package manager
to install those dependencies. I want to call it three things here. First, is library dependency. There's the express package again
with the exact version number so that npm knows which version
to download and install. Second is command to start. This tells you how to start the node
program to run the application. And third is required Node.js runtime. The package.json file specifies
a Node.js version to use. To summarize, you learned from exploring
the source code that this example application requires these five
components to run the application. One, the run time. Two, dependencies, you need to install
them before you can run the app. Three, the source code, JavaScript files. Four, the index.html file or
static assets in general. In the real world, you are likely to have
images, CSS files, and more html files. And five, configuration. You'll need a way to
start the application. In this example, it's just node server.js
but this can be more complicated. If you look at an example Python
application, this will look more or less the same. Python is an interpreted language so
you need a runtime, Python, to run it. In python applications you specify
dependencies with the requirements.txt file and you usually install dependencies
using the package manager, pip. Your source code will be Python files and the command to start your application is
python server.py, or something similar. Let's take this a step further and look at
an example for a compiled language, Java. The source code is no longer
required to run the application but the compiled binaries are. You'll need to compile the sources first. Here's another example using Go. Note that when you use Go,
you don't need a runtime. As you build your application,
you install dependencies and compile them together with
the source code to a binary. Optionally, you can even embed
the assets into the binary. There's one final class of dependencies
I haven't mentioned yet, and that's system packages. Some applications have dependencies and system tools that can't be expressed
as an application library dependency. Examples include a headless browser,
tools you might want to use to download or process files, additional system fonts,
and ImageMagick. Which is a suite of open source programs
that can be used to convert Images from one format to another and
process them. In development, you might need
additional tooling to aid in debugging. You might also need a shell and
debugging tools such as Xdebug for PHP. To put everything together, these are the types of files you
might need to run your application, any application and
what you might find in a container image. System packages, a runtime, dependencies,
source code, binaries, and static assets. Your application might not
need everything on this list. You might just have one binary and
that's that. The last item is
the container configuration. The container configuration details how to
turn a container image into a container, a running process. In all of the previous examples I listed
just the command you needed to start to run the application as configuration. And that is usually an important part but
there is more. The command you need to run
is called the entrypoint. Some other important settings
are environment variables, usually used to pass configuration
settings to your application. A working directory,
where to start the app. And the user to run the program with. It's of particular
importance to set the user, as the default is to use the root user,
the system administrator user. And that's risky from a security viewpoint
as this will contain more permissions than you need to run your application. That's why you don't want to
miss customizing this setting. You can override the program argument and
environment variables after you build the container image,
when you start the container. Here's what's important to
remember about container images. A container image is a package. It has your application and
everything your application needs to run, which can include an entire runtime,
assets and container configuration that points
out what program to start and how. A minimal container Image only has one
program file and a command to run it. Different languages may need
a runtime configured as well. For example, Java, Python and Node.js. Your application might need additional
system dependencies to work. For example, a headless browser. Those are also included in the image. The container image is a self contained
package with your application and everything it needs to run.